<template>
    <div class="layout-route">
        <div class="card card-search">
            <el-form :model="queryParams" ref="queryForm" v-show="showSearch" inline label-width="100px">
                <el-form-item label="运营" prop="admin_id">
                    <el-select v-model="queryParams.admin_id" placeholder="请选择运营" clearable size="small" style="width: 240px">
                        <el-option v-for="(dict, index) in salesmanList" :key="index" :label="dict.nickname" :value="dict.id" />
                    </el-select>
                </el-form-item>
                <el-form-item label="月份" prop="date">
                    <el-date-picker type="monthrange" v-model="queryParams.date" start-placeholder="开始时间" end-placeholder="结束时间"></el-date-picker>
                </el-form-item>
                <el-form-item label=" ">
                    <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
                    <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
                </el-form-item>
            </el-form>
        </div>

        <div class="card">
            <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" icon="el-icon-document" @click="showCheckDialog">报表核对</el-button>
            </el-col>
                <el-col :span="1.5">
                    <el-button type="warning" plain icon="el-icon-download" :loading="download" :disabled="download" @click="handleExport">导出</el-button>
                </el-col>
                <right-toolbar :showSearch.sync="showSearch" :columns="columns" @queryTable="getList"></right-toolbar>
            </el-row>
            <!-- <el-tabs v-model="activeTab" @tab-click="handleStatus" type="border-card">
                <el-tab-pane v-for="(item, index) in status" :key="index" :label="item.label" :name="item.value.toString()"> </el-tab-pane>
            </el-tabs> -->
            <el-table v-loading="loading" :data="tableData" border script>
                <el-table-column label="月份" v-if="showColumn('date')" prop="date" min-width=""></el-table-column>
                <el-table-column label="运营" v-if="showColumn('admin_name')" prop="admin_name" min-width=""></el-table-column>
                <el-table-column label="发货单量" v-if="showColumn('count')" prop="count" min-width=""></el-table-column>
                <el-table-column label="原始销售额$" v-if="showColumn('total_price_old')" prop="total_price_old" min-width="100"></el-table-column>
                <el-table-column label="已发货销售额$" v-if="showColumn('total_price')" prop="total_price" min-width="120"></el-table-column>
                <el-table-column label="94%销售额$" v-if="showColumn('total_price_handle')" prop="total_price_handle" min-width="120"></el-table-column>
                <el-table-column label="账单物流费$" v-if="showColumn('freight')" prop="freight" min-width="100"></el-table-column>
                <el-table-column label="采购成本$" v-if="showColumn('material')" prop="material" min-width="100"></el-table-column>
                <el-table-column label="物流加成$" v-if="showColumn('LogisticsMarkupFee')" prop="LogisticsMarkupFee" min-width="100"></el-table-column>
                <el-table-column label="广告费$" v-if="showColumn('advertising')" prop="advertising" min-width="100"></el-table-column>
                <el-table-column label="争议成本$" v-if="showColumn('dispute')" prop="dispute" min-width="100"></el-table-column>
                <!-- <el-table-column label="小店利润$" v-if="showColumn('small_profit')" prop="small_profit" min-width="100"></el-table-column> -->
                <el-table-column label="个人盈利$" v-if="showColumn('large_profit')" prop="large_profit" min-width="100"></el-table-column>
                <!-- <el-table-column label="渠道总利润" v-if="showColumn('chananel_total_shop')" prop="chananel_total_shop" min-width="100"></el-table-column> -->
                <!-- <el-table-column label="渠道分配比例" v-if="showColumn('advertising_rate')" prop="advertising_rate" min-width="100"></el-table-column> -->
                <!-- <el-table-column label="利润分配$" v-if="showColumn('chananel_total')" prop="chananel_total" min-width="100"></el-table-column> -->
                <el-table-column label="最终利润$" v-if="showColumn('commission_old')" prop="commission_old" min-width="100"></el-table-column>
                <el-table-column label="提成比例" v-if="showColumn('get_channel_rate')" prop="get_channel_rate" min-width="100"></el-table-column>
                <el-table-column label="当月美金汇率" v-if="showColumn('dollar')" prop="dollar" min-width="100"></el-table-column>
                <el-table-column label="提成金额￥" v-if="showColumn('commission')" prop="commission" min-width="100"></el-table-column>
                <el-table-column label="操作" fixed="right">
                    <template slot-scope="scope">
                        <el-button type="text" icon="el-icon-edit" @click="showDetailDialog(scope.row)">明细</el-button>
                        <!-- <el-button type="text" icon="el-icon-download" @click="handleExport(scope.row)">导出</el-button> -->
                    </template>
                </el-table-column>
            </el-table>
            <pagination v-show="total > 0" :total="total" :page.sync="pageInfo.page" :limit.sync="pageInfo.limit" @pagination="getList"></pagination>
        </div>
        
        <AdminCheckDialog v-if="checkDialog.visible" v-model="checkDialog.visible" :params="checkDialog.data"></AdminCheckDialog>
        
        <detail-dialog v-model="detailDialog.visible" v-if="detailDialog.visible" :params="detailDialog.data" @success="handleQuery"></detail-dialog>
    </div>
</template>

<script>
import { queryMonthlyPersonalList, getSalesmanList, exportMonthlyPersonalList,exportMonthlyPersonalInfo } from '@/api/shop'
import { exportFile } from '@/utils/index'
import { parseTime } from '@/utils/quickadmin'
import DetailDialog from './components/DetailDialog.vue'
import AdminCheckDialog from '../components/AdminCheckDialog.vue'
export default {
    name: 'MonthlyProfitList',
    components: { AdminCheckDialog,DetailDialog },
    data() {
        return {
            // 遮罩层
            loading: true,
            download: false,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 表格数据
            tableData: [],
            // 店铺列表下拉
            salesmanList: [],
            pageInfo: {
                page: 1,
                limit: 10,
            },
            // 查询参数
            queryParams: {
                status: undefined,
                admin_id: undefined,
                date: [],
            },
            checkDialog: {
                visible: false,
                data: {}
            },
            detailDialog: {
                visible: false,
                data: {},
            },
            status: [
                { label: '全部', value: '' },
                { label: '个人部分', value: 'PersonalPart' },
                { label: '公司部分', value: 'CompanySection' },
            ],
            activeTab: '',
            columns: [
                {label:'月份', prop: 'date', visible: true },
                {label:'运营', prop: 'admin_name', visible: true },
                {label:'发货单量', prop: 'count', visible: true },
                {label:'原始销售额$', prop: 'total_price_old', visible: true },
                {label:'已发货销售额$', prop: 'total_price', visible: true },
                {label:'94%销售额$', prop: 'total_price_handle', visible: true },
                {label:'账单物流费$', prop: 'freight', visible: true },
                {label:'采购成本$', prop: 'material', visible: true },
                {label:'物流加成$', prop: 'LogisticsMarkupFee', visible: true },
                {label:'广告费$', prop: 'advertising', visible: true },
                {label:'争议成本$', prop: 'dispute', visible: true },
                {label:'小店利润$', prop: 'small_profit', visible: true },
                {label:'大店利润$', prop: 'large_profit', visible: true },
                {label:'渠道总利润$', prop: 'chananel_total_shop', visible: true },
                // {label:'渠道分配比例', prop: 'advertising_rate', visible: true },
                {label:'渠道个人利润$', prop: 'chananel_total', visible: true },
                {label:'个人总利润$', prop: 'commission_old', visible: true },
                {label:'提成比例', prop: 'get_channel_rate', visible: true },
                {label:'当月美金汇率', prop: 'dollar', visible: true },
                {label:'提成金额￥', prop: 'commission', visible: true },
            ]
        }
    },
    async created() {
        this.loading = true
        try {
            const salesmanData = await getSalesmanList()
            this.salesmanList = salesmanData?.data?.list ?? []
        } catch (error) {
            this.loading = false
            console.log(error)
        }
        this.getList()
    },
    methods: {
        handleStatus(tab) {
            this.queryParams.status = tab.name
            this.getList()
        },
        showColumn(prop) {
            const data = this.columns.find(c => c.prop === prop)
            return data?.visible ?? true
        },
        showDetailDialog(row) {
            this.detailDialog.visible = true
            this.detailDialog.data = row
        },
        showCheckDialog() {
            this.checkDialog.visible = true
        },
        handleExport(row) {
            this.download = true
            const qyparams = {
                ...this.pageInfo,
                ...this.queryParams,
            }
            if (qyparams.date?.length == 2) {
                qyparams.date = [parseTime(qyparams.date[0], 'y-m'), parseTime(qyparams.date[1], 'y-m')]
            }
            // const qyparams = {
            //     ...row,
            //     // date: [row.date]
            // }
            exportMonthlyPersonalInfo(qyparams)
                .then(res => {
                    exportFile(res, () => {
                        this.download = false
                    })
                })
                .catch(err => {
                    this.download = false
                })
        },
                //列表
        getList() {
            this.loading = true
            const qyparams = {
                ...this.pageInfo,
                ...this.queryParams,
            }
            if (qyparams.date?.length == 2) {
                qyparams.date = [parseTime(qyparams.date[0], 'y-m'), parseTime(qyparams.date[1], 'y-m')]
            }
            queryMonthlyPersonalList(qyparams)
                .then(response => {
                    this.tableData = response.data.list
                    this.total = response.data.total
                })
                .finally(() => {
                    this.loading = false
                })
        },

        // 搜索按钮操作
        handleQuery() {
            this.pageInfo.page = 1
            this.getList()
        },

        // 重置按钮操作
        resetQuery() {
            this.resetForm('queryForm')

            this.handleQuery()
        },
    },
}
</script>
